package com.tucc.hello.leetcode.no1_400.no1_200.no61_80.no64;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] grid = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
        System.out.println(solution.minPathSum(grid));
    }

    public int minPathSum(int[][] grid) {
        int m = grid.length;
        if (m == 0) {
            return 0;
        }
        int n = grid[0].length;
        if (n == 0) {
            return 0;
        }
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int min = Integer.MAX_VALUE;
                if (i - 1 >= 0) {
                    min = Math.min(dp[i - 1][j], min);
                }
                if (j - 1 >= 0) {
                    min = Math.min(dp[i][j - 1], min);
                }
                if (min == Integer.MAX_VALUE) {
                    min = 0;
                }
                dp[i][j] = min + grid[i][j];
            }
        }
        return dp[m - 1][n - 1];
    }
}
